package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Students;
import com.ruoyi.system.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/students")
public class StudentsController extends BaseController {

    @Autowired
    private StudentsService studentsService;

    @PreAuthorize("@ss.hasPermi('system:students:list')")
    @GetMapping("/list")
    public TableDataInfo list(Students students)
    {
        startPage();
        List<Students> list = studentsService.selectStudentList(students);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:students:query')")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable Long studentId)
    {
        return AjaxResult.success(studentsService.selectStudentById(studentId));
    }

    @PreAuthorize("@ss.hasPermi('system:students:add')")
    @Log(title = "学生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Students students)
    {
        students.setCreateBy(getUsername());
        return toAjax(studentsService.insertStudent(students));
    }

    @PreAuthorize("@ss.hasPermi('system:students:edit')")
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Students students)
    {
        students.setUpdateBy(getUsername());
        return toAjax(studentsService.updateStudent(students));
    }

    @PreAuthorize("@ss.hasPermi('system:students:remove')")
    @Log(title = "学生管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds)
    {
        return toAjax(studentsService.deleteStudentByIds(studentIds));
    }

}
